export default {
    SET (state,data){
        const {totalPrice,totalMount,cartData} = data; //解析出来
        state.totalPrice = totalPrice;
        state.totalMount = totalMount;
        state.cartData = cartData;
    },
    CART(state,payload){
        const {list,num} = payload
        const {id,price,title,type,desc,url} = list
        state.totalMount += num
        state.totalPrice += num*list.price
        const index = state.cartData.findIndex(item =>item.id === id);
        if(index === -1){
            state.cartData.push({id,price,title,type,desc,url,choose:true,totalMount:num,totalPrice:num*price})
        }else{
            state.cartData[index].totalMount += num
            state.cartData[index].totalPrice += num*list.price
        }
    },
    CHOOSE(state,payload){
        const {id,choose} = payload
        const index = state.cartData.findIndex(item =>item.id === id);
        state.cartData[index].choose = !choose
        if(state.cartData[index].choose) {
            state.totalPrice += state.cartData[index].totalPrice
            state.totalMount += state.cartData[index].totalMount
        }else{
            state.totalPrice -= state.cartData[index].totalPrice
            state.totalMount -= state.cartData[index].totalMount
        }
    },
    SET_CART(state,payload) {
        const {id,price,type} = payload
        const index = state.cartData.findIndex(item =>item.id === id);
        switch(type){
            case 'PLUS':
            if(state.cartData[index].totalMount < 20){
                state.cartData[index].totalMount += 1;
                state.cartData[index].totalPrice += price
                state.totalPrice += price;
                state.totalMount += 1;
            }
                break;
            case 'MINUS':
                if(state.cartData[index].totalMount>1){
                    state.cartData[index].totalPrice -= price;
                    state.cartData[index].totalMount -= 1;
                    state.totalPrice -= price;
                    state.totalMount -= 1;
                }
                break;
            default:
                break; 
        }    
    },
    DEL(state,payload) {
        const {id} = payload
        const index = state.cartData.findIndex(item =>item.id === id);
        state.totalPrice -= state.cartData[index].totalPrice;
        state.totalMount -= state.cartData[index].totalMount;
        state.cartData = state.cartData.filter(item => item.id !== id);
    }
}
